#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 文件名：05-循环.py

print "while 循环，条件成立，就循环执行，条件不成立，就执行else"
count = 9
while count > 1:
    print "+连接"
    print "count=" + str(count)

    print "，连接"
    print "count=", count
    count -= 1
else:
    print "小于1了"


 # 一般循环中加入if结合使用
i = 1
while i < 10:
    if i % 2 == 0:
        print i, "是偶数"
    else:
        print i, "是奇数"
    i += 1


print "continue跳过本次循环，继续下一次"
while (i < 10):
    if i % 2 == 0:
        continue
    print i, "是奇数"
    i += 1


'''# 判断条件为常量，表示必然成立，死循环,终止调试
while 1:
    input1 = raw_input("请输入幸运数字")
    print input1
'''

print "break终止整个循环"
a = 1
while 1:
    print "a=", a
    a += 1
    if a > 10:
        break


# ===for===

print "迭代,直接遍历出每一项"
for letter in "abcd":
    print "当前字符是{0}".format(letter)

fruits = ['apple', 'orange', 'banana']

print "水果列表的长度", len(fruits)

for fruit in fruits:    
    print "当前水果", fruit

# 打印range序列
rlist1 = range(4)

for i in rlist1:
    print i

print "通过索引index打印水果列表"

for index in range(len(fruits)):
    print fruits[index]
    

print "打印10-20以类的素数"
for num in range(10, 20):
    for i in range(2, num):
        if num % i == 0:
            j = num / i
            print "{0}={1}*{2}".format(num, j, i)
            break
    else:
        print num, "是素数"

        
# 嵌套 循环中有循环
print "打印1-100之间的素数"
'''
先遍历每个1-100每个数字，
对每个数字进行素数的判断
'''
i = 2
while i <= 100:
# 判断素数，在2-自身之间能整除的的都不是素数
    j = 2
    while j < (i / 2):
        if i % j == 0:
            break
        j += 1
    else:
        print "{0}是素数".format(i)

# 下一个数
    i += 1
    

# break 退出循环
# continue 跳过本次循环
num3 = 10
while num3 > 1:
    num3 -= 1
    '''if num3 == 3:
        print "遇到{0}退出整个循环".format(num3)
        break'''
    if num3 == 3:
        print "遇到{0}退出本次循环".format(num3)
        continue
    print "当前的数字是{0}".format(num3)
    

print "迭代,直接遍历出每一项"
for letter in "abcd":
    '''if letter == "c":
        print "遇到字符c终止循环\n"
        break'''
    if letter == "c":
        print "遇到字符c退出本次循环\n"
        continue
    print "当前字符是{0}".format(letter)